import os

import cv2

from option import parse_args


def main():
    args = parse_args()
    crops = os.path.join(args.root_dir, 'crops')
    lines = open('abnormal_width_statistics.dat').readlines()
    for line in lines:
        line_split = line.split(',')
        assert len(line_split) == 5
        max_width = int(line_split[4].strip())
        # original
        remove_abnormal_width_image(max_width, os.path.join(crops, line_split[1]))
        # fake
        remove_abnormal_width_image(max_width, os.path.join(crops, line_split[2]))


def remove_abnormal_width_image(max_width, video_path):
    for crop in os.listdir(video_path):
        img = cv2.imread(os.path.join(video_path, crop), cv2.IMREAD_GRAYSCALE)
        diff = max_width - img.shape[1]
        if img.shape[1] < 100 and diff > 50:
            os.remove(os.path.join(video_path, crop))


if __name__ == '__main__':
    main()
